<template>
  <div class="login_container">
    <div class="login_box">
      <div class="logo">
        <img src="./../assets/logo.png" alt="">
      </div>
      <!-- 登录区域-->
      <div class="login">
        <el-form ref="loginFormRef" :model="loginForm" label-width="0" :rules="loginFormRules">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <span>{{$store.state.count}}</span>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      // 登录表单数据
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单数据
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 表单登录功能
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        // 如果验证不通过
        // eslint-disable-next-line no-useless-return
        if (!valid) return
        // 如果验证通过发送请求
        let { data: res } = await this.$http.post(`login`, this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        // 将登录成功之后的token保存到sessionStorage中
        window.sessionStorage.setItem('token', res.data.token)
        // 登录成功之后跳转到home页面
        await this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang="less">
  .login_container {
    background-color: #2b4b6b;
    height: 100%;

    .login_box {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 450px;
      height: 320px;
      background-color: #fff;
      border-radius: 3px;

      .logo {
        position: absolute;
        left: 50%;
        width: 130px;
        height: 130px;
        border-radius: 50%;
        background-color: #fff;
        transform: translate( -50%, -50%);
        padding: 10px;
        box-sizing: border-box;
        border: 1px solid #ccc;
        box-shadow: 0 0 10px #ddd;
        img{
          display: block;
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background-color: #eee;
        }
      }
      .login{
        margin-top: 100px;
        padding: 0 20px ;
        box-sizing: border-box;
        .btns{
          display: flex;
          justify-content: flex-end;
        }
      }
    }
  }
</style>
